﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Chianti.Service.Types;
namespace Chianti.Service
{
    public class PositionCalculator
    {
        public static double GetDistanceInMeters(double _long1, double _lat1, double _long2, double _lat2)
        {
            //TODO: need to supposrt cross boundary routes (across the equator or prime meridian)
            double long1 = Math.Abs(_long1);
            double lat1 = Math.Abs(_lat1);
            double long2 = Math.Abs(_long2);
            double lat2 = Math.Abs(_lat2);

            double dLatInRad = (lat2 - lat1) * (Math.PI / 180);
            double dLongInRad = (long2 - long1) * (Math.PI / 180);

            double a = Math.Sin(dLatInRad / 2) * Math.Sin(dLatInRad / 2) +
                            Math.Cos(lat1 * Math.PI / 180) * Math.Cos(lat2 * Math.PI / 180) *
                            Math.Sin(dLongInRad / 2) * Math.Sin(dLongInRad / 2);

            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double result = Constants.RADIUS_OF_EARTH_METERS * c;
            return result;
        }

        public static double GetDistanceInMeters(Position longitude1, Position latitude1, Position longitude2, Position latitude2)
        {
            double long1 = longitude1.ToDecimal();
            double lat1 = latitude1.ToDecimal();
            double long2 = longitude2.ToDecimal();
            double lat2 = latitude2.ToDecimal();
            return GetDistanceInMeters(long1, lat1, long2, lat2);
           

        }
    }
}